/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config;

import opiam.admin.faare.config.javabeans.JBViewDefs;

import org.apache.commons.digester.Digester;

import org.apache.log4j.Logger;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import java.io.IOException;
import java.io.InputStream;


/**
 * Views service configuration loading from the XML file.
 *
 * The XML file must contain the following lines :
 * <!DOCTYPE mapping PUBLIC
 *         "-//OPIAM ADMIN//DTD Views Service Configuration 1.0//EN"
 *         "http://www.opiam.org/admin/dtd/views_conf.dtd">
 *
 * The second line is registered to the digester (it does not need to be an actual URL).
 *
 */
public final class ViewsConfig implements ErrorHandler
{
    /** Log4J. */
    private static Logger _logger = Logger.getLogger(ViewsConfig.class.getName());

    /** DOCTYPE key. */
    public static final String VIEWS_CONFIG_DTD_KEY = "-//OPIAM ADMIN//DTD Views Service Configuration 1.0//EN";

    /** DOCTYPE URL. */
    public static final String VIEWS_CONFIG_DTD_URL = "/config/service/views/views_conf.dtd";

    /** XML Parser. */
    private static Digester _digester = null;

    /** Current instance. */
    private static ViewsConfig _instance = null;

    /** Parser exception. */
    private static SAXParseException _saxParseException = null;

    /**
     * Constructor for ProfilesDigester.
     */
    private ViewsConfig()
    {
        super();
    }

    /**
     * Digester initialization with DTD fields.
     */
    private static void initProfileDigester()
    {
        _saxParseException = null;
        _digester = new Digester();

        _digester.setErrorHandler(_instance);
        _digester.setValidating(true);

        _digester.register(VIEWS_CONFIG_DTD_KEY,
            ViewsConfig.class.getResource(VIEWS_CONFIG_DTD_URL).toString());

        _digester.addObjectCreate("viewdefs",
            "opiam.admin.faare.config.javabeans.JBViewDefs");
        _digester.addSetProperties("viewdefs");

        _digester.addObjectCreate("viewdefs/viewdef",
            "opiam.admin.faare.config.javabeans.JBViewDef");
        _digester.addSetNext("viewdefs/viewdef", "addViewDef",
            "opiam.admin.faare.config.javabeans.JBViewDef");
        _digester.addSetProperties("viewdefs/viewdef");

        _digester.addCallMethod("viewdefs/viewdef/param", "addParam", 2);
        _digester.addCallParam("viewdefs/viewdef/param/param-name", 0);
        _digester.addCallParam("viewdefs/viewdef/param/param-value", 1);

        _digester.addObjectCreate("viewdefs/viewdef/viewdef_profiles",
            "opiam.admin.faare.config.javabeans.JBViewDefProfiles");
        _digester.addSetNext("viewdefs/viewdef/viewdef_profiles",
            "setJbViewDefProfiles",
            "opiam.admin.faare.config.javabeans.JBViewDefProfiles");

        _digester.addObjectCreate("viewdefs/viewdef/viewdef_profiles/viewdef_profile",
            "opiam.admin.faare.config.javabeans.JBViewDefProfile");
        _digester.addSetNext("viewdefs/viewdef/viewdef_profiles/viewdef_profile",
            "addViewDefProfile",
            "opiam.admin.faare.config.javabeans.JBViewDefProfile");
        _digester.addSetProperties(
            "viewdefs/viewdef/viewdef_profiles/viewdef_profile");

        /*_digester.addCallMethod("viewdefs/viewdef/param", "addParam", 2);
        _digester.addCallParam("viewdefs/viewdef/param/param-name", 0);
        _digester.addCallParam("viewdefs/viewdef/param/param-value", 1);
        */
        _digester.addObjectCreate("viewdefs/viewdef/objectview",
            "opiam.admin.faare.config.javabeans.JBObjectView");
        _digester.addSetNext("viewdefs/viewdef/objectview", "addObjectView",
            "opiam.admin.faare.config.javabeans.JBObjectView");
        _digester.addSetProperties("viewdefs/viewdef/objectview");

        _digester.addObjectCreate("viewdefs/viewdef/objectview/orgChartAtt",
            "opiam.admin.faare.config.javabeans.JBOrgChartAtt");
        _digester.addSetNext("viewdefs/viewdef/objectview/orgChartAtt",
            "addOrgChartAtt",
            "opiam.admin.faare.config.javabeans.JBOrgChartAtt");
        _digester.addSetProperties("viewdefs/viewdef/objectview/orgChartAtt");

        _digester.addObjectCreate("viewdefs/viewdef/level",
            "opiam.admin.faare.config.javabeans.JBLevel");
        _digester.addSetNext("viewdefs/viewdef/level", "addLevel",
            "opiam.admin.faare.config.javabeans.JBLevel");
        _digester.addSetProperties("viewdefs/viewdef/level");

        _digester.addObjectCreate("viewdefs/viewdef/level/levelRessource",
            "opiam.admin.faare.config.javabeans.JBLevelRessource");
        _digester.addSetNext("viewdefs/viewdef/level/levelRessource",
            "addLevelRessource",
            "opiam.admin.faare.config.javabeans.JBLevelRessource");
        _digester.addSetProperties("viewdefs/viewdef/level/levelRessource");

        _digester.addObjectCreate("viewdefs/viewdef/level/levelRessource/link",
            "opiam.admin.faare.config.javabeans.JBLink");
        _digester.addSetNext("viewdefs/viewdef/level/levelRessource/link",
            "addLink", "opiam.admin.faare.config.javabeans.JBLink");
        _digester.addSetProperties("viewdefs/viewdef/level/levelRessource/link");

//DW/2600/BeginPatch
        _digester.addObjectCreate("viewdefs/orgchartparams",
            "opiam.admin.faare.config.javabeans.JBOrgChartParams");
        _digester.addSetNext("viewdefs/orgchartparams", "setOrgChartParams",
            "opiam.admin.faare.config.javabeans.JBOrgChartParams");
//DW/2615/BeginPatch
        _digester.addSetProperties("viewdefs/orgchartparams");
//DW/2615/EndPatch

        _digester.addObjectCreate("viewdefs/orgchartparams/cellColor",
            "opiam.admin.faare.config.javabeans.JBColor");
        _digester.addSetNext("viewdefs/orgchartparams/cellColor", "setCellColor",
            "opiam.admin.faare.config.javabeans.JBColor");
        _digester.addSetProperties("viewdefs/orgchartparams/cellColor");
//DW/2600/EndPatch

//DW/2673/BeginPatch
        _digester.addObjectCreate("viewdefs/orgchartparams/backgroundColor",
        "opiam.admin.faare.config.javabeans.JBColor");
    _digester.addSetNext("viewdefs/orgchartparams/backgroundColor", "setBackgroundColor",
        "opiam.admin.faare.config.javabeans.JBColor");
    _digester.addSetProperties("viewdefs/orgchartparams/backgroundColor");
//DW/2673/EndPatch
        
//DW/2630/BeginPatch
        _digester.addObjectCreate("viewdefs/viewdef/level/levelResource",
            "opiam.admin.faare.config.javabeans.JBLevelRessource");
        _digester.addSetNext("viewdefs/viewdef/level/levelResource",
            "addLevelRessource",
            "opiam.admin.faare.config.javabeans.JBLevelRessource");
        _digester.addSetProperties("viewdefs/viewdef/level/levelResource");

        _digester.addObjectCreate("viewdefs/viewdef/level/levelResource/link",
            "opiam.admin.faare.config.javabeans.JBLink");
        _digester.addSetNext("viewdefs/viewdef/level/levelResource/link",
            "addLink", "opiam.admin.faare.config.javabeans.JBLink");
        _digester.addSetProperties("viewdefs/viewdef/level/levelResource/link");
//DW/2630/EndPatch
    }

    /**
     * Gets ViewsConfig instance.
     *
     * @return ViewsConfig instance
     */
    public static ViewsConfig getInstance()
    {
        if (_instance == null)
        {
            _instance = new ViewsConfig();
            initProfileDigester();
        }

        return _instance;
    }

    /**
     * Gets views config.
     *
     * @param in configuration stream
     *
     * @return views config
     *
     * @throws IOException exception while reading stream.
     * @throws SAXException exception while parsing data.
     */
    public JBViewDefs readConfig(InputStream in)
        throws IOException, SAXException
    {
        JBViewDefs result = null;
        result = (JBViewDefs) _digester.parse(in);

        if (_saxParseException != null)
        {
            _logger.debug("Exception : " + _saxParseException.getMessage());
            throw new SAXException(_saxParseException.getMessage());
        }

        return result;
    }

    /**
     * Parsing error callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void error(SAXParseException saxParseException)
    {
        _logger.debug(" error exception!!!!!!!!!!!!!!!");
        _saxParseException = saxParseException;
    }

    /**
     * Parsing fatal error callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void fatalError(SAXParseException saxParseException)
    {
        _logger.debug("fatalError exception!!!!!!!!!!!!!!!");
        _saxParseException = saxParseException;
    }

    /**
     * Parsing warning callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void warning(SAXParseException saxParseException)
    {
        _logger.debug("warning : " + saxParseException.getMessage());
    }
}
